﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.eforceglobal.crux.configuration.DTOs;
using com.eforceglobal.crux.security;
using com.eforceglobal.crux.controllers;

public partial class Controls_DashboardItems : System.Web.UI.UserControl
{
    public enum ItemRepeatDirection { Vertical = 0, Horizontal }
    private ICruxSecurityManager cruxSecurityManager;
    private bool _includeImage = true;
    private string _dashBosrdName = "";

    public string DashBosrdName
    {
        get { return _dashBosrdName; }
        set { _dashBosrdName = value; }
    }

    public ItemRepeatDirection ItemOrientation
    {
        set
        {
            if (value == ItemRepeatDirection.Vertical)
                dtlDashboardItems.RepeatDirection = RepeatDirection.Vertical;
            else
                dtlDashboardItems.RepeatDirection = RepeatDirection.Horizontal;
        }
    }

    public bool IncludeIcon
    {
        set { _includeImage = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
            SessionData sessionData = new SessionData();
            string UserName = sessionData.CurrentUser.UserName;
            string CompanyName = sessionData.CurrentUser.CompanyName;
            string sectionName = sessionData.CurrentViewSection;

            DashboardInformation dashboard = cruxSecurityManager.GetDashboard(UserName, CompanyName,_dashBosrdName);
            dtlDashboardItems.DataSource = dashboard.DasboardItems;
            dtlDashboardItems.DataBind();
            if (dashboard.DasboardItems != null) lblAddOns.Visible = true;
        }
    }

    protected void dtlDashboardItems_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DashboardItemInformation dashboardItem = (DashboardItemInformation)e.Item.DataItem;
            LinkButton lnk = (LinkButton)e.Item.FindControl("lnk");
            Image img = (Image)e.Item.FindControl("img");
            lnk.Text = dashboardItem.Title;

            string postBackUrl = ReplaceTagsInViewUrl(dashboardItem.View.ViewUrl);
            lnk.PostBackUrl =  postBackUrl;
            if (dashboardItem.View.IsDashboard)
            {
                string appendCharacter = "?";
                if (lnk.PostBackUrl.Contains("?")) appendCharacter = "&";
                lnk.PostBackUrl = lnk.PostBackUrl + appendCharacter + UILiterals.DashboardName + "=" + dashboardItem.View.DashboardName;
            }
            
            if (_includeImage)
            {
                if (dashboardItem.View.ViewImageUrl == cruxSecurityManager.GetApplicationPath())
                    img.ImageUrl = cruxSecurityManager.GetApplicationPath() + "/appimages/no_image.JPG";
                else
                    img.ImageUrl = dashboardItem.View.ViewImageUrl;
            }
        }
    }

    private string ReplaceTagsInViewUrl(string url)
    {
        string objectid = Request.Params["ID"];
        url = url.Replace("[ID]", objectid);
        url = url.Replace("[id]", objectid);
        return url;
    }
}